#include "comm.hpp"
#include "log.hpp"

using namespace std;

int main(){
    // 创建
    int shmid = CreateShm();
    // log(Debug, "creat shm done");
    // sleep(1);

    // 挂起
    char *shmaddr = (char *)shmat(shmid, nullptr, 0);
    // log(Debug, "shmat shm done");
    // sleep(2);

    struct shmid_ds shmds;
    shmctl(shmid, IPC_STAT, &shmds);
    cout << "shm size: " << shmds.shm_segsz << endl;
    cout << "shm nattch: " << shmds.shm_nattch << endl;
    cout << "shm mode: " << shmds.shm_perm.mode << endl;
    printf("shm key: 0x%x\n", shmds.shm_perm.__key);

    // IPC code
    while(true){
        cout << "client say@" << shmaddr << endl;
        sleep(1);
    }

    // 去关联
    shmdt(shmaddr);
    // log(Debug, "shmdt shm done");
    // sleep(2);

    // 释放
    shmctl(shmid, IPC_RMID, nullptr);
    // log(Debug, "shmctl shm done");
    // sleep(2);

    return 0;
}